/** 
 * 数字转货币格式 
 * @param value 数字
 * @returns 货币格式字符串
 * */
export const toCurrency = (value?: number | string) => {
    return toCurrencyNum(value, '$');
}

export const toCurrencyNum = (value: number | string | undefined | null, currency: string, decimal: number = 2) => {
    if (!value) return currency + (decimal > 0 ? `0.${'0'.repeat(decimal)}` : '0');
    const formattedValue = parseFloat(`${value}`).toFixed(decimal);
    return currency + (decimal > 0 
        ? formattedValue.replace(/\d(?=(\d{3})+\.)/g, '$&,') 
        : formattedValue.replace(/\d(?=(\d{3})+$)/g, '$&,'));
}